#include <iostream>
#include <deque>

int main() {
    int n, k;
    std::cin>>n>>k;
    std::deque<int> dq;
    for (int i = 0; i < n; ++i) {
        dq.push_back(i);
    }
    while (dq.size() > 1) {
    
        for (int i = 1; i < k; ++i) {
            int person = dq.front();
            dq.pop_front();
            dq.push_back(person);
        }
       
        dq.pop_front();
    }

    std::cout<<dq.front()<<std::endl;

    return 0;
}